package com.dev.op.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dev.op.entity.ProblemTag;
import com.dev.op.entity.Tag;
import com.dev.op.enums.ResultCode;
import com.dev.op.exceptions.BizException;
import com.dev.op.service.ProblemTagService;
import com.dev.op.mapper.ProblemTagMapper;
import com.dev.op.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【problem_tag】的数据库操作Service实现
* @createDate 2025-03-29 22:40:33
*/
@Service
public class ProblemTagServiceImpl extends ServiceImpl<ProblemTagMapper, ProblemTag>
    implements ProblemTagService{

    @Autowired
    private TagService tagService;

    @Autowired
    private ProblemTagMapper problemTagMapper;

    @Override
    public List<Tag> getTagsByProblemId(String problemId) {
        return problemTagMapper.selectTagsByProblemId(problemId);
    }


    //搜,找差集-》删除，找左余集，添加
    @Override
    @Transactional()
    public Boolean updateProblemTag(String problemId, List<Tag> tags) {

        if(tags == null || tags.isEmpty()) {
            return this.deleteProblemTagAllTag(problemId);
        }

        //补上id
        for(Tag tag : tags) {
            if(tag.getTagName()==null){
                throw new BizException(ResultCode.NOT_FOUND);
            }
            if(tag.getTagId()==null){
                String id = this.getTagIdByTagName(tag.getTagName());
                if(id.equals("-1")){
                    throw new BizException(ResultCode.NOT_FOUND);
                }
                tag.setTagId(id);
            }
        }
        boolean del = this.deleteProblemTagAllTag(problemId);
        boolean add = this.addProblemTagAllTag(problemId, tags);
        return del && add;
    }

    @Override
    @Transactional
    public Boolean deleteProblemTagByTagEntity(String problemId, List<Tag> tags) {
        if(tags == null || tags.isEmpty()) {
            return true;
        }
        List<ProblemTag> batch = new ArrayList<>();
        for(Tag tag: tags){
            ProblemTag problemTag = new ProblemTag(problemId,tag.getTagId());
            batch.add(problemTag);
        }
        return this.removeBatchByIds(batch);
    }

    @Override
    @Transactional
    public Boolean addProblemTagByTagEntity(String problemId, List<Tag> tags) {
        if(tags == null || tags.isEmpty()) {
            return true;
        }
        List<ProblemTag> batch = new ArrayList<>();
        for(Tag tag: tags){
            ProblemTag problemTag = new ProblemTag(problemId,tag.getTagId());
            batch.add(problemTag);
        }

        problemTagMapper.saveBatchPlus(batch);
        return true;
    }

    @Override
    @Transactional
    public Boolean deleteProblemTagAllTag(String problemId) {
        LambdaQueryWrapper<ProblemTag> qw = new LambdaQueryWrapper<>();
        qw.eq(ProblemTag::getProblemId, problemId);
        return this.remove(qw);
    }

    @Override
    public Boolean addProblemTagAllTag(String problemId, List<Tag> tags) {
        List<ProblemTag> batch = new ArrayList<>();
        for(Tag tag: tags){
            ProblemTag problemTag = new ProblemTag(problemId,tag.getTagId());
            batch.add(problemTag);
        }
        problemTagMapper.saveBatchPlus(batch);
        return true;
    }

    @Override
    public String getTagIdByTagName(String tagName) {
        LambdaQueryWrapper<Tag> qw = new LambdaQueryWrapper<>();
        qw.eq(Tag::getTagName, tagName);
        Tag tag = tagService.getOne(qw);
        if(tag == null){
            return "-1";
        }
        else{
            return tag.getTagId();
        }
    }
}




